from typing import Any, List, Optional
from fastapi import APIRouter, Depends, HTTPException, status, Query
from fastapi_pagination import Page
from pydantic.types import UUID4
from sqlalchemy.orm import Session
from app import crud, models, schemas
from app.api import deps
from app.models.model_training import ModelTraining
router = APIRouter()


@router.get("/", summary="获取自训练模型列表")
def read_self_training_model(
        db: Session = Depends(deps.get_db),
        with_archived: Optional[bool] = False,
        page_size: int = Query(10, alias="page_size"),  # 每页显示多少条数据
        current_page: int = Query(1, alias="current_page")  # 当前页码
) -> Any:
    """
    检索所有的自训练模型。
    """
    query = db.query(ModelTraining)
    query = crud.model_training.filter_archivable(query, with_archived)
    # 计算总记录数
    total = query.count()
    # 分页查询
    self_training_models = query.offset((current_page - 1) * page_size).limit(page_size).all()
    result = []
    for training_model in self_training_models:
        updated_at = training_model.updated_at if training_model.updated_at else training_model.created_at
        updated_at_str = updated_at.strftime("%Y年%m月%d日 %H:%M:%S")
        result.append({
            "id": training_model.id,
            "model_version": training_model.model_version,
            "training_text": training_model.training_text,
            "training_voiceprint": training_model.training_voiceprint,
            "text_accuracy": training_model.text_accuracy,
            "sentence_accuracy": training_model.sentence_accuracy,
            "core_word_accuracy": training_model.core_word_accuracy,
            "long_text_accuracy": training_model.long_text_accuracy,
            "realtime_wer": training_model.realtime_wer,
            "recognize_text": training_model.recognize_text,
            "comprehensive_accuracy": training_model.comprehensive_accuracy,
            "updated_at": updated_at_str,
        })

    return {"total": total, "items": result}


